package com.recallgo.manage.web.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.recallgo.manage.entity.Item;
import com.recallgo.manage.entity.vo.PageVO;
import com.recallgo.manage.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/rest/item")
public class ItemController {

    @Autowired
    private ItemService itemService;

    /**
     * 添加商品
     * @param item
     * @param desc
     * @return
     */
    @RequestMapping(value = "/addItem", method = RequestMethod.POST)
    public ResponseEntity<Void> addItem(Item item, String desc, String itemParams){
        itemService.saveItem(item, desc,itemParams);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改商品
     * @param item
     * @param desc
     * @return
     */
    @RequestMapping(value = "/updateItem", method = RequestMethod.POST)
    public ResponseEntity<Void> updateItem(Item item, String desc, String itemParams){
        itemService.modItem(item, desc, itemParams);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 查询商品信息
     * @param currPage
     * @param size
     * @return
     */
    @RequestMapping(value = "/queryItem",method = RequestMethod.GET)
    public ResponseEntity<PageVO<Item>> queryItem(@RequestParam(defaultValue = "1", name = "page") Integer currPage, @RequestParam(defaultValue = "10", name = "rows") Integer size){
        Page<Item> page = new Page<>();
        page.setCurrent(currPage);
        page.setSize(size);
        EntityWrapper<Item> wrapper = new EntityWrapper<>();
        wrapper.where("status = {0}", 1).or("status = {0}", 2);
        wrapper.orderBy("created", false);
        Page<Item> itemPage = itemService.selectPage(page, wrapper);

        PageVO<Item> pageVO = new PageVO<>();
        pageVO.setTotal((int) itemPage.getTotal());
        pageVO.setRows(itemPage.getRecords());
        return ResponseEntity.ok(pageVO);
    }

    /**
     * 下架商品
     * @param ids
     * @return
     */
    @RequestMapping(value = "/instock", method = RequestMethod.POST)
    public ResponseEntity<Integer> instock(Long[] ids){
        List<Item> items = new ArrayList<>();
        for(int i=0; i<ids.length; i++){
            Item item = new Item();
            item.setId(ids[i]);
            item.setStatus(2);
            items.add(item);
        }
        itemService.insertOrUpdateBatch(items);
        return ResponseEntity.ok(200);
    }

    /**
     * 上架商品
     * @param ids
     * @return
     */
    @RequestMapping(value = "/reshelf", method = RequestMethod.POST)
    public ResponseEntity<Integer> reshelf(Long[] ids){
        List<Item> items = new ArrayList<>();
        for(int i=0; i<ids.length; i++){
            Item item = new Item();
            item.setId(ids[i]);
            item.setStatus(1);
            items.add(item);
        }
        itemService.insertOrUpdateBatch(items);
        return ResponseEntity.ok(200);
    }

    /**
     * 删除商品
     * @param ids
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseEntity<Integer> delete(Long[] ids){
        List<Item> items = new ArrayList<>();
        for(int i=0; i<ids.length; i++){
            Item item = new Item();
            item.setId(ids[i]);
            item.setStatus(3);
            items.add(item);
        }
        itemService.insertOrUpdateBatch(items);
        return ResponseEntity.ok(200);
    }
}
